package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.service.AddService;
import model.service.AddServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class ajaxServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String bookNo = request.getParameter("bookNo");

        //和数据库中的数据进行比对，判断bookNo是否已经存在
        AddService addService=new AddServiceImpl();
        boolean flag=addService.isExist(bookNo);

        Map<String,Object> map=new HashMap<>();
        if(flag)//书籍已经存在
        {
            map.put("bookNoExist",true);
        }
        else
        {
            map.put("bookNoExist",false);
        }

        //将map转换为json，并且传递给客户端
        ObjectMapper mapper=new ObjectMapper();
        mapper.writeValue(response.getWriter(),map);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
              doPost(request,response);
    }
}
